//
//  PlayerViewContoroller.m
//  MantuPlay
//
//  Created by Markus Sintonen on 14.8.2009.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "PlayerViewContoroller.h"
#import "TrackDatabase.h"
#import "Track.h"
#import "NSOperationQueueSharedQueue.h"

#define SONG_INFO_LABEL(label,frame,color) label = [[UILabel alloc] initWithFrame:frame]; \
									label.font = [UIFont fontWithName:@"Verdana-Bold" size:11.0]; \
									label.backgroundColor = [UIColor clearColor]; \
									label.textColor = color; \
									label.shadowColor = [UIColor blackColor]; \
									label.shadowOffset = CGSizeMake(-1,-1); \
									label.textAlignment = UITextAlignmentCenter; \

@implementation PlayerViewContoroller

- (id)initWithTrackPlaylist:(NSArray*)playlist {
	if ((self = [super init])) {
		self.hidesBottomBarWhenPushed = YES;
		self.navigationItem.hidesBackButton = YES;
		
		currentTrack = 0;
		trackQueue = [[NSArray alloc] initWithArray:playlist];
		
		AudioEngine *audioEngine = [AudioEngine sharedAudioEngine];
		[audioEngine setDelegate:self];
		[audioEngine setupTrackPlayerWithTrackPath:[[trackQueue objectAtIndex:currentTrack] getCompleteFilePath]];
		//[audioEngine playTrackQueue];
	}
	return self;
}

- (void)loadView {
	UIView *navBarCustomView = [[UIView alloc] initWithFrame:CGRectMake(0.0,0.0,209.0,42.0)];
	[navBarCustomView setBackgroundColor:[UIColor clearColor]];
	self.navigationItem.titleView = navBarCustomView;
	[navBarCustomView release];

	UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 42.0, 30.0)];
	backButton.adjustsImageWhenHighlighted = NO;
	[backButton setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"BackArrowButtonUp" ofType:@"png"]] forState:UIControlStateNormal];
	[backButton setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"BackArrowButtonDown" ofType:@"png"]] forState:UIControlStateHighlighted];
	[backButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
	UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
	self.navigationItem.leftBarButtonItem = backButtonItem;
	[backButton release];
	[backButtonItem release];
	
	UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"LeftArrow" ofType:@"png"]]
									style:UIBarButtonItemStylePlain target:self action:@selector(backButtonPressed)];
	self.navigationItem.rightBarButtonItem = infoButton;
	[infoButton release];
	
	SONG_INFO_LABEL(artistLabel, CGRectMake(0.0, 0.0, 209.0, 42.0/3.0), [UIColor colorWithRed:0.576 green:0.576 blue:0.576 alpha:1.0])
	SONG_INFO_LABEL(titleLabel, CGRectMake(0.0, 42.0/3.0-2.0, 209.0, 42.0/3.0), [UIColor whiteColor])
	SONG_INFO_LABEL(albumLabel, CGRectMake(0.0, 42.0/3.0*2.0-4.0, 209.0, 42.0/3.0), [UIColor colorWithRed:0.576 green:0.576 blue:0.576 alpha:1.0])
	[navBarCustomView addSubview:artistLabel];
	[navBarCustomView addSubview:titleLabel];
	[navBarCustomView addSubview:albumLabel];
	[artistLabel release];
	[titleLabel release];
	[albumLabel release];
	
	self.navigationController.navigationBar.barStyle = (UIBarStyle)1;

	UIView *mainView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
	mainView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
	mainView.backgroundColor = [UIColor whiteColor];
	
	albumArtView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 320.0)];
	albumArtView.backgroundColor = [UIColor blackColor];
	albumArtView.contentMode = UIViewContentModeScaleAspectFit;
	[mainView addSubview:albumArtView];
	[albumArtView release];
	
	UIImageView *playerControlsView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 320.0, 320.0, 95.0)];
	playerControlsView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"PlayerControlsBackground" ofType:@"png"]];
	[mainView addSubview:playerControlsView];
	[playerControlsView release];
	
	self.view = mainView;
	[mainView release];
}

-(void)trackChanged {
	currentTrack++;
	
	Track *track = [trackQueue objectAtIndex:currentTrack];
	artistLabel.text = track.artist;
	titleLabel.text = track.title;
	albumLabel.text = track.album;
	
	[self loadImageForAlbum:track.album toImageView:albumArtView];
}
-(NSString*)nextTrackFilePath {
	return [[trackQueue objectAtIndex:currentTrack + 1] getCompleteFilePath];
}

-(void)loadImageForAlbum:(NSString*)album toImageView:(UIImageView*)imageView {
	AlbumArtLoadOperation *albumArtLoadOperation;
	albumArtLoadOperation = [[AlbumArtLoadOperation alloc] initWithImageView:imageView albumName:album imageSize:CGSizeMake(320.0, 320.0)];
	[[NSOperationQueue sharedOperationQueue] addOperation:albumArtLoadOperation];
	[albumArtLoadOperation release];
}

-(void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
	self.navigationController.navigationBar.barStyle = (UIBarStyle)0;
	[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
	[[NSOperationQueue sharedOperationQueue] cancelAllOperations];
}

-(void)backButtonPressed {
	[[self navigationController] popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


- (void)dealloc {
	[[AudioEngine sharedAudioEngine] setDelegate:nil];
	[trackQueue release];
    [super dealloc];
}


@end
